﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using System.Reflection;

namespace Settings
{
	
	// Summary description for Settings.
	
	public class Settings
	{
        private static NameValueCollection m_settings;
        private static string m_settingsPath;

        public static void Read( string settingsPath )
        {
            m_settings = new NameValueCollection();

            if( 0 == settingsPath.Length )
            {
                try
                {
                    Module mod = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0];
                    m_settingsPath = Path.GetDirectoryName(mod.FullyQualifiedName);
                    m_settingsPath += @"\";
                    m_settingsPath += Path.GetFileNameWithoutExtension(mod.FullyQualifiedName);
                    m_settingsPath += @"_INI.xml";
                }
                catch (Exception e)
                {
                    m_settingsPath = string.Empty;
                    Console.WriteLine("The *_INI.xml Directory Defining failed: {0}", e.ToString());
                }
            }
            else
                m_settingsPath = settingsPath;

            if (File.Exists(m_settingsPath))
            {
                System.Xml.XmlDocument xdoc = new XmlDocument();
                xdoc.Load(m_settingsPath);
                XmlElement root = xdoc.DocumentElement;
                System.Xml.XmlNodeList nodeList = root.ChildNodes.Item(0).ChildNodes;

                // Add settings to the NameValueCollection.


                // WriteLog
                int count = 0;
                while (count < nodeList.Count)
                {
                    if ("WriteLog" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("WriteLog", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("WriteLog", "True");

                // AutoStart
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("AutoStart" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("AutoStart", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("AutoStart", "False");

                // SmartStep
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("SmartStep" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("SmartStep", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("SmartStep", "False");

                // ClientNumber
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("ClientNumber" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("ClientNumber", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("ClientNumber", "000156498");

                // Instrument
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("Instrument" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("Instrument", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("Instrument", "EUR/USD");

                // InitialLots
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("InitialLots" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("InitialLots", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("InitialLots", "1");

                // MaxDouble
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("MaxDouble" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("MaxDouble", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("MaxDouble", "4");

                // PipsStep
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("PipsStep" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("PipsStep", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("PipsStep", "20");

                // BaseOrderID
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("BaseOrderID" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("BaseOrderID", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("BaseOrderID", "0");

                // BaseLots
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("BaseLots" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("BaseLots", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("BaseLots", "1");

                // BasePrice
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("BasePrice" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("BasePrice", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("BasePrice", "0");

                // BaseBuyOrSell
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("BaseBuyOrSell" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("BaseBuyOrSell", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("BaseBuyOrSell", "0");

                // StopOrderID
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("StopOrderID" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("StopOrderID", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("StopOrderID", "0");

                // StopLots
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("StopLots" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("StopLots", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("StopLots", "1");

                // StopPrice
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("StopPrice" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("StopPrice", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("StopPrice", "0");

                // StopBuyOrSell
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("StopBuyOrSell" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("StopBuyOrSell", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("StopBuyOrSell", "1");

                // BuyPrice = 0;
                while (count < nodeList.Count)
                {
                    if ("BuyPrice" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("BuyPrice", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("BuyPrice", "0");

                // SellPrice
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("SellPrice" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("SellPrice", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("SellPrice", "1");

                // FailedCount
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("FailedCount" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("FailedCount", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("FailedCount", "0");

                // TopBorder
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("TopBorder" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("TopBorder", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("TopBorder", "0");

                // BottomBorder
                count = 0;
                while (count < nodeList.Count)
                {
                    if ("BottomBorder" == nodeList.Item(count).Attributes["key"].Value)
                    {
                        m_settings.Add("BottomBorder", nodeList.Item(count).Attributes["value"].Value);
                        break;
                    }
                    count++;
                }
                if (count == nodeList.Count)// not found
                    m_settings.Add("BottomBorder", "0");

            }
            else // file does not exists - set defaults
            {
                m_settings.Add("WriteLog", "True");
                m_settings.Add("AutoStart", "False");
                m_settings.Add("SmartStep", "False");
                m_settings.Add("ClientNumber", "000156498");
                m_settings.Add("Instrument", "EUR/USD");
                m_settings.Add("MaxDouble", "4");
                m_settings.Add("InitialLots", "1" );
                m_settings.Add("PipsStep", "20" );
                m_settings.Add("BaseOrderID", "0");
                m_settings.Add("BaseLots", "1");
                m_settings.Add("BaseBuyOrSell", "0");
                m_settings.Add("BasePrice", "1");
                m_settings.Add("StopOrderID", "0");
                m_settings.Add("StopLots", "1");
                m_settings.Add("StopBuyOrSell", "1");
                m_settings.Add("StopPrice", "1");
                m_settings.Add("BuyPrice", "0");
                m_settings.Add("SellPrice", "0");
                m_settings.Add("FailedCount", "0");
                m_settings.Add("TopBorder", "0");
                m_settings.Add("BottomBorder", "0");
            }

            Save();
        }

		public static void Save()
		{
            XmlTextWriter tw = new XmlTextWriter( m_settingsPath, System.Text.UTF8Encoding.UTF8 );
			tw.WriteStartDocument();
			tw.WriteStartElement( "configuration" );
			tw.WriteStartElement( "appSettings" );

            for ( int i = 0; i < m_settings.Count; ++i )
			{
				tw.WriteStartElement( "add" );
				tw.WriteStartAttribute( "key", string.Empty );
                tw.WriteRaw( m_settings.GetKey( i ) );
				tw.WriteEndAttribute();

				tw.WriteStartAttribute( "value", string.Empty );
                tw.WriteRaw( m_settings.Get( i ) );
				tw.WriteEndAttribute();
				tw.WriteEndElement();
			}
			tw.WriteEndElement();
			tw.WriteEndElement();
			tw.Close();
		}

        public static string WriteLog
        {
            get { return m_settings.Get( "WriteLog" ); }
            set { m_settings.Set( "WriteLog", value ); }
        }

        public static string AutoStart
        {
            get { return m_settings.Get("AutoStart"); }
            set { m_settings.Set("AutoStart", value); }
        }

        public static string SmartStep
        {
            get { return m_settings.Get("SmartStep"); }
            set { m_settings.Set("SmartStep", value); }
        }

        public static string ClientNumber
        {
            get { return m_settings.Get("ClientNumber"); }
            set { m_settings.Set("ClientNumber", value); }
        }

        public static string Instrument
        {
            get { return m_settings.Get("Instrument"); }
            set { m_settings.Set("Instrument", value); }
        }

       public static string InitialLots
        {
            get { return m_settings.Get("InitialLots"); }
            set { m_settings.Set("InitialLots", value); }
        }

       public static string MaxDouble
        {
            get { return m_settings.Get("MaxDouble"); }
            set { m_settings.Set("MaxDouble", value); }
        }

        public static string PipsStep
        {
            get { return m_settings.Get("PipsStep"); }
            set { m_settings.Set("PipsStep", value); }
        }

        public static string BaseOrderID
        {
            get { return m_settings.Get("BaseOrderID"); }
            set { m_settings.Set("BaseOrderID", value); }
        }

        public static string BaseLots
        {
            get { return m_settings.Get("BaseLots"); }
            set { m_settings.Set("BaseLots", value); }
        }

        public static string BaseBuyOrSell
        {
            get { return m_settings.Get("BaseBuyOrSell"); }
            set { m_settings.Set("BaseBuyOrSell", value); }
        }

        public static string BasePrice
        {
            get { return m_settings.Get("BasePrice"); }
            set { m_settings.Set("BasePrice", value); }
        }

       public static string StopOrderID
        {
            get { return m_settings.Get("StopOrderID"); }
            set { m_settings.Set("StopOrderID", value); }
        }

        public static string StopLots
        {
            get { return m_settings.Get("StopLots"); }
            set { m_settings.Set("StopLots", value); }
        }

        public static string StopBuyOrSell
        {
            get { return m_settings.Get("StopBuyOrSell"); }
            set { m_settings.Set("StopBuyOrSell", value); }
        }

        public static string StopPrice
        {
            get { return m_settings.Get("StopPrice"); }
            set { m_settings.Set("StopPrice", value); }
        }

       public static string SellPrice
        {
            get { return m_settings.Get("SellPrice"); }
            set { m_settings.Set("SellPrice", value); }
        }

       public static string BuyPrice
        {
            get { return m_settings.Get("BuyPrice"); }
            set { m_settings.Set("BuyPrice", value); }
        }

       public static string FailedCount
       {
           get { return m_settings.Get("FailedCount"); }
           set { m_settings.Set("FailedCount", value); }
       }

       public static string TopBorder
       {
           get { return m_settings.Get("TopBorder"); }
           set { m_settings.Set("TopBorder", value); }
       }

       public static string BottomBorder
       {
           get { return m_settings.Get("BottomBorder"); }
           set { m_settings.Set("BottomBorder", value); }
       }
    }
}
